/**
 * Created by w2ex on 7/1/16.
 */
var path = require('path');
var webpack = require('webpack');
var webappRoot = path.join(__dirname, 'src');
var buildRoot = path.join(__dirname, 'dist');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var fs = require('fs');

module.exports = {
    context: webappRoot,

    entry: './app.js',

    output: {
        path: buildRoot,
        filename: 'app.bundle-[hash].js'
    },

    devtool: 'source-map',

    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader?presets[]=es2015&presets[]=react'
            },
            {
                test: /\.scss$/,
                loaders: ["style", "css", "sass"]
            },
            {
                test: /\.font\.(json|js)$/,
                loader: 'style!css!fontgen?embed&types=woff'
            },
            {
                test: /\.(png|jpg|gif)$/,
                loader: 'url?limit=2000'
            }
        ]
    },

    plugins: [
        new CopyWebpackPlugin([
            {
                from: path.join(webappRoot, 'index.html'),
                to: path.join(buildRoot, 'index.html')
            },
            {
                from: path.join(webappRoot, 'favicon.ico'),
                to: path.join(buildRoot, 'favicon.ico')
            },
            {
                from: path.join(webappRoot, 'server.js'),
                to: path.join(buildRoot, 'server.js')
            },
            {
                from: path.join(webappRoot, 'robots.txt'),
                to: path.join(buildRoot, 'robots.txt')
            },
            {
                from: path.join(webappRoot, 'start.sh'),
                to: path.join(buildRoot, 'start.sh')
            },
            {
                from: path.join(webappRoot, 'stop.sh'),
                to: path.join(buildRoot, 'stop.sh')
            },
            {
                from: path.join(webappRoot, 'package.json'),
                to: path.join(buildRoot, 'package.json')
            }
        ], {
            copyUnmodified: true
        }),
        new webpack.optimize.DedupePlugin(),
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.optimize.UglifyJsPlugin({
            minimize: true,
            sourceMap: false,
            output: {
                comments: false
            },
            compress: {
                warnings: false
            }
        }),
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': JSON.stringify('production')
            }
        }),

        function() {
            this.plugin("done", function(stats) {
                // customized plugin to replace js path with hash in prod env
                // @see https://github.com/webpack/webpack/issues/86

                var hash = stats.hash;

                var replaceInFile = function (filePath, toReplace, replacement) {
                    var replacer = function (match) {
                        console.log('Replacing in %s: %s => %s', filePath, match, replacement);
                        return replacement
                    };
                    var str = fs.readFileSync(filePath, 'utf8');
                    var out = str.replace(new RegExp(toReplace, 'g'), replacer);
                    fs.writeFileSync(filePath, out);
                };

                replaceInFile(path.join(buildRoot, 'index.html'),
                    'bundle.js',
                    'bundle-' + hash + '.js'
                );
            });
        }
    ]
}
